﻿//排班管理详细计划清单
Ext.namespace('FantastPlatform', 'FantastPlatform.UI');
FantastPlatform.UI.AddCarGrid = function (config) {
    Ext.apply(this, config);

    var fileStore = new Ext.data.Store({
        proxy: new Ext.data.HttpProxy({
            url: '../Service/PersonService.aspx?Action=GetCarsByPerson'
        }),
        reader: new Ext.data.JsonReader({
            root: 'topics',
            totalProperty: 'totalCount',
            id: 'Id',
            fields: ['Id', 'Number', 'CarType', 'CreateDate', 'Enable', 'CreatePoliceCode']
        }),
        remoteStore: true
    });
    this.store = fileStore;
    var carStore = new Ext.data.SimpleStore({
        fields: ['Name', 'Value'],
        data: [['小车', '小车'], ['摩托车', '摩托车'], ['大车', '大车']]
    });
    var carComboBox = new Ext.form.ComboBox({
        fieldLabel: '车辆类型',
        name: 'CarType',
        hiddenName: 'CarType',
        store: carStore,
        displayField: 'Name',
        mode: 'local',
        valueField: 'Value',
        allowBlank: true,
        editable: false,
        triggerAction: 'all',
        emptyText: '选择车辆类型',
        readOnly: true,
        anchor: '90%'
    });
    var paperBar = new FantastPlatform.Common.PapeToolBar({
        store: fileStore,
        defaultNum: 8
    });
    this.paperBar = paperBar;
    this.bbar = paperBar;
    this.pageNumber = paperBar.pageSize;

    var nm = new Ext.grid.RowNumberer();
    var sm = new Ext.grid.CheckboxSelectionModel();

    this.columns = [nm, sm, {
        header: 'Id',
        dataIndex: 'Id',
        hidden: true
    },
    {
        header: '车牌',
        dataIndex: 'Number',
        editor: new Ext.form.TextField({ anchor: '90%' }),
        hidden: false
    },
    {
        header: '类型',
        dataIndex: 'CarType',
        editor: carComboBox,
        hidden: false
    },
    {
        header: '创建时间',
        dataIndex: 'CreateDate',
        hidden: true
    },
    {
        header: '是否有效',
        dataIndex: 'Enable',
        hidden: true
    }];
    FantastPlatform.UI.AddCarGrid.superclass.constructor.call(this, {
        loadMask: { msg: '正在加载数据...' },
        sm: sm
    });
};
Ext.extend(FantastPlatform.UI.AddCarGrid, Ext.grid.EditorGridPanel, {
    onRender: function (ct, position) {
        FantastPlatform.UI.AddCarGrid.superclass.onRender.call(this, ct, position);
    },
    //刷新
    onUpdataCarClick: function (CreatePoliceCode, Id) {
        this.store.removeAll();
        this.store.proxy.conn = { method: 'post', url: this.url };
        this.store.load({ params: { start: 0, limit: this.paperBar.getPageNumber(), dir: '', sort: '', id: Id} });
        if (CreatePoliceCode != policemanNumber || !btnCarEnable) {
            Ext.getCmp('addCarDetail').setDisabled(true);
            Ext.getCmp('deleteCarDetail').setDisabled(true);
            Ext.getCmp('SaveCarDetail').setDisabled(true);
        }
    },
    //查看详细信息
    onCheckDetailsClick: function () {
        var node = checkGridSelected(this);
        if (node) {
            if (node.length > 1) {
                showErrorMessage("操作失败", "只能选择一条数据进行查看", "");
            } else {
                var fId = node[0].get('Id');
                var id = GUID();
                currtWind = openWind('../InfoCollection/CarDetails.aspx?Id=' + fId, id, '查看', 480, 530, null);
            }
        }
    },
    //添加
    onSaveCarClick: function (sender, grid, wind) {
        var id = GUID();
        var myMask = new Ext.LoadMask(sender.getEl(), {
            msg: "正在保存数据,请稍候...."
        });
        var records = sender.store.getRange();
        var dataStr1 = '{"Cars":[';
        for (var i = 0; i < records.length; i++) {
            dataStr1 += '{"Id":"' + records[i].get("Id") + '", "Number":"' + records[i].get("Number") + '","CarType":"' + records[i].get("CarType") + '", "Enable":"' + records[i].get("Enable") + '", "CreateDate":"' + records[i].get("CreateDate") + '"';
            dataStr1 += '},';
        }
        if (dataStr1 && records.length != 0) {
            dataStr1 = dataStr1.substring(0, dataStr1.length - 1);
        }
        dataStr1 += "]}";
        var url = '../Service/PersonService.aspx?Action=AddCar';
        myMask.show();
        Ext.Ajax.request({
            method: 'post',
            url: url,
            params: { data: dataStr1, id: sender.Id },
            success: onSuccess,
            failure: onFailure
        });

        function onFailure(request, options) {
            myMask.hide();
            showErrorMessage('操作失败', request.responseText, '');
        }

        function onSuccess(request, options) {
            // 服务器返回json形式的结果
            myMask.hide();
            var reText = request.responseText;
            var reTextObj = Ext.util.JSON.decode(reText);
            if (eval(reTextObj.success)) {
                showMessage('操作成功', '保存成功');
                if (grid)
                    updata(grid);
                if (wind)
                    wind.close();
            } else {
                showErrorMessage('操作失败', reTextObj.message, '');
            }
        }
    }

});